﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TerhelesEloszto.Common;
using TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class DeleteUser : System.Web.UI.Page
	{
		private static IEngine engine = Engine.Instance();
		protected void Page_Load( object sender, EventArgs e )
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			QuestionLabel.Text = Request.QueryString["Q"] + "?";

		}

		protected void OKbtn_Click( object sender, EventArgs e )
		{
			if( Request.QueryString["T"] != null )
			{
				if( Request.QueryString["T"].Equals( "userdelete" ) )
				{
					IUser currentUser = null;
					foreach( IAdmin actAdmin in engine.GetAdmins() )
					{
						if( actAdmin.Neptun.Trim().Equals( Request.QueryString["ID"].Trim() ) )
						{
							currentUser = actAdmin;
							break;
						}
					}
					if( currentUser == null )
					{
						foreach( ITeacher actTeacher in engine.GetTeachers() )
						{
							if( actTeacher.Neptun.Trim().Equals( Request.QueryString["ID"].Trim() ) )
							{
								currentUser = actTeacher;
								break;
							}
						}
					}
					if( currentUser == null ) Response.Redirect( "~/Admin/User.aspx?msg=0" );
					try
					{
						engine.RemoveUser( currentUser );
						Response.Redirect( "~/Admin/User.aspx?msg=1" );
					}
					catch( EngineException ex )
					{
						Response.Redirect( "~/Admin/User.aspx?msg=0" );
					}

				}
				else if( Request.QueryString["T"].Equals( "factdelete" ) )
				{
					IFact currentFact = null;
					foreach( IFact actFact in engine.GetFacts() )
					{
						if( actFact.Neptun.Equals( Request.QueryString["ID"] ) )
						{
							currentFact = actFact;
							break;
						}
					}
					if( currentFact == null ) Response.Redirect( "~/Admin/Fact.aspx?msg=0" );
					try
					{
						engine.RemoveFact( currentFact );
						Response.Redirect( "~/Admin/Fact.aspx?msg=1" );
					}
					catch( EngineException ex )
					{
						Response.Redirect( "~/Admin/Fact.aspx?msg=0" );
					}
				}
				else if( Request.QueryString["T"].Equals( "taskdelete" ) )
				{
					ITask currentTask = null;
					foreach( ITask actTask in engine.GetTasks() )
					{
						if( actTask.ID == Convert.ToInt32( Request.QueryString["ID"] ) )
						{
							currentTask = actTask;
							break;
						}
					}
					if( currentTask == null ) Response.Redirect( "~/Admin/Task.aspx?msg=0" );
					try
					{
						engine.RemoveTask( currentTask );

						Response.Redirect( "~/Admin/Task.aspx?msg=1" );
					}
					catch( EngineException ex )
					{
						Response.Redirect( "~/Admin/Task.aspx?msg=0" );
					}
				}
				else if( Request.QueryString["T"].Equals( "skilldelete" ) )
				{
					ISkill currentSkill = null;
					foreach( ISkill actSkill in engine.GetSkills() )
					{
						if( actSkill.ID == Convert.ToInt32( Request.QueryString["ID"] ) )
						{
							currentSkill = actSkill;
							break;
						}
					}
					if( currentSkill == null ) Response.Redirect( "~/Admin/Skills.aspx?msg=0" );
					try
					{
						engine.RemoveSkill( currentSkill );

						Response.Redirect( "~/Admin/Skills.aspx?msg=1" );
					}
					catch( EngineException ex )
					{
						Response.Redirect( "~/Admin/Skills.aspx?msg=0" );
					}
				}
				else if( Request.QueryString["T"].Equals( "intervaldelete" ) )
				{
					ITask currentTask = null;
					foreach( ITask actTask in engine.GetTasks() )
					{
						if( actTask.ID == Convert.ToInt32( Request.QueryString["ID"] ) )
						{
							currentTask = actTask;
							break;
						}
					}
					if( currentTask == null ) Response.Redirect( "~/Admin/Task.aspx?msg=0" );
					try
					{
						currentTask.TimeIntervals.RemoveAt( Convert.ToInt32( Request.QueryString["Interval"] ) );
						engine.UpdateTask( currentTask, currentTask.GetRelatedFact(), currentTask.Name, currentTask.Type, currentTask.RequiredSkills, currentTask.TimeIntervals, currentTask.Semester );
					}
					catch( Exception ex )
					{
						Response.Redirect( "~/Admin/Task.aspx?msg=0" );
					}
					Response.Redirect( "~/Admin/Task.aspx?msg=1" );
				}
			}

		}

		protected void cancelBtn_Click( object sender, EventArgs e )
		{
			Response.Redirect( "~/Admin/AdminDefault.aspx" );
		}

	}
}